/**
 * 游客登录按钮控件
 */
var GuestLoginBtnUnit = LoginBtnUnit.extend({
    _reciveCodeKey: EventKey.ReceiveGuestCode,
    _ccsFileName: res.GuestBtnUnit_json,
    initUnit: function () {
        LoginBtnUnit.prototype.initUnit.call(this);
        this._nodeList['text_uid'].setVisible(!cc.sys.isNative);
    },

    redirectLogin: function () {
        LoginBtnUnit.prototype.redirectLogin.call(this);
        var uid = this._nodeList['text_uid'].getString();
        if (isNullString(uid)) {
            uid = DeviceUtil.fetchUDID() + 'A';
        }
        eventManager.dispatchEvent(this._reciveCodeKey, uid);
    },
    _receiveCode: function (code) {
        LoginBtnUnit.prototype._receiveCode.call(this);
        var self = this;
        var sex = 1;
        if (code && code.length < 6) {
            var codeint = parseInt(code);
            if (_.isNumber(codeint)) sex = codeint % 3;
            if (!sex) sex = 1;
        }
        var response = {
            openid: code,
            nickname: '游客' + code,
            sex: sex,
            province: '',
            city: '',
            country: '',
            headimgurl: res.defaultHead,
            unionid: code
        };
        if (!cc.sys.isNative && code && code.indexOf('#') == 0) {
            code = code.split('#')[1];
            var url = 'http://auth.yygameapi.com:44440/' + app.authPath + '/unionid?UserID=' + code + '&sign=password';
            http.get(url, function (data) {
                // cc.log(data.indexOf("找不到"));
                if (data.indexOf("找不到") == 0 || data == "") {
                    self.doLogin(response);
                } else {
                    var userInfo = JSON.parse(data);
                    showLoading("正在登录游戏..");
                    self.doLogin(userInfo);
                }
            }, function () {
                // cc.log("查找失败");
                self.doLogin(response);
            });
            return;
        }
        this.doLogin(response);
    }
});